<?php

namespace app\task\model;

use app\merchant\model\MerchantModel;
use think\admin\Model;


/**
 * 订单任务模型
 * @class TaskModel
 * @package app\task\model
 */
class TaskModel extends Model
{
    protected $name = 'task';

    /**
     * 字典-状态类型
     */
    public const TASK_STATUS__PENDING_SUBMISSION = 0;
    public const TASK_STATUS__PENDING_REVIEW = 1;
    public const TASK_STATUS__PENDING_ACCEPTANCE = 2;
    public const TASK_STATUS__PENDING_COMPLETION = 3;
    public const TASK_STATUS__COMPLETED = 4;
    public const TASK_STATUS__REVIEW_FAILED = -1;
    public const TASK_STATUS__CANCELED = -2;
    public const TASK_STATUS_DICT = [
//        self::TASK_STATUS__PENDING_SUBMISSION => '待提交',
        self::TASK_STATUS__PENDING_REVIEW => '待审核',
        self::TASK_STATUS__PENDING_ACCEPTANCE => '待接单',
        self::TASK_STATUS__PENDING_COMPLETION => '待完成',
        self::TASK_STATUS__COMPLETED => '已完成',
        self::TASK_STATUS__REVIEW_FAILED => '审核不通过',
        self::TASK_STATUS__CANCELED => '已取消',
    ];

    /**
     * 状态获取器
     * @param $value
     * @param $data
     * @return string
     */
    public function getTaskStatusTextAttr($value, $data)
    {
        return self::TASK_STATUS_DICT[$data['status']] ?? '';
    }

    /**
     * 关联订单任务分类表
     */
    public function category(){
        return $this->belongsTo(CategoryModel::class, 'category_id', 'id');
    }

    /**
     * 关联发单商户表
     */
    public function merchant(){
        return $this->belongsTo(MerchantModel::class, 'merchant_id', 'id');
    }

    /**
     * 关联执行订单
     */
    public function taskOrder(){
        return $this->hasOne(OrderModel::class, 'id', 'order_id');
    }

    /**
     * 关联货运信息
     */
    public function transport(){
        return $this->hasOne(TransportModel::class, 'task_id', 'id');
    }
}
